home *** CD-ROM | disk | FTP | other *** search
- Path: rznews.rrze.uni-erlangen.de!news
- From: elmar.vogt@rzmail.uni-erlangen.de (Elmar Vogt)
- Newsgroups: comp.lang.c++
- Subject: Re: Question about header files
- Date: 6 Mar 1996 13:06:38 GMT
- Organization: LUR, University of Erlangen.
- Message-ID: <4hk2ku$irl@rznews.rrze.uni-erlangen.de>
- References: <4hg50t$924@vodka.intele.net>
- NNTP-Posting-Host: faust207.lstm.uni-erlangen.de
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <4hg50t$924@vodka.intele.net>, chalain@mcr.net says...
- >
- >Anyone feeling generous? I have a question that wants an
- essay-style
- >answer....
- >
-
- I'll give it a shot.
-
- >
- >In the main file (main.cpp), put the line
- >
- >#include "myheader.h";
- >
- >and in myheader.cpp, put the line
- >
- >#include "myheader.h";
- >
- >From this, I assume that the compiler, upon opening a *.h file, will
- >check for a *.c or *.cpp file of the same name, yes?
-
- Nope. It's just that at compile time, the line
- #include "myheader.h"
- will be replaced by the contents of said file, line for line.
- There's not much more to it.
-
- >
- >This confuses me just a little bit. However, it gets worse.
- >>
- >I assign a pointer to the mode 0x13 video memory with the line
- >
- >unsigned char far *video_buffer = (unsigned char far *)0xA0000000L;
- >
- > [in one header file].
- I would *really* like to put this line either in the header file or
- in
- >the code file for graphics, but if I do I can't get access to the
- pointer
- >in MAINFILE or any of its other dependents.
-
- I'm not sure, but I'm afraid you've defined the same variable twice
- (assigning it two startup values, which confuses the compiler,
- though they are the same).
- Option a):
- Write
-
- unsigned char far *video_buffer = (unsigned char far *)0xA0000000L;
-
- in the .CPP file of your graphics routine, and
-
- extern unsigned char far *video_buffer;
-
- in all the other source files, or
-
- Option b):
- Write
-
- unsigned char far *video_buffer;
-
- in your graphics .H file, and assign it a value in a proper
- intialization routine that's invoked before video_buffer is used:
-
- video_buffer = (unsigned char far *)0xA0000000L;
-
-
- Alternatively, you could #define it, since it's constant anyway:
-
- #define VIDEO_BUFFER unsigned...
-
- (Does this work with a pointer? It's not an LValue this way, is it?)
-
- (I'd recommend using the _const_ clause, BTW!)
-
- >Be aware that I am using Symantec C++ and am compiling from the DOS
- >prompt,
- >so there aren't any IDE options or project resources that I can add
- or
- >tinker with.
-
- No, but you should have a lot of compiler options or switches at
- hand. Look at the reference manual for the options.
-
- >
- >Secondly, I have an extension of my set of graphics function to cope
- with
- >PCX graphic files. These functions are in PCXFILE.H and
- PCXFILE.CPP.
- >I would like to include these files as is, but right now the code
- just
- >will not compile. I either get "identifier already defined" at
- compile
- >time if I put #include MYGRAPH and #include PCXFILE in MAINFILE or
- >"unknown
- >identifier" at link time if I put #include PCXFILE inside MYGRAPH.H.
-
- If MYGRAPH includes PCXFILE, obviously what happens is:
-
- MAINFILE -> MYGRAPH -> PCXFILE
- +-----> PCXFILE,
-
- ie the same header file is included twice. (Nested includes work
- straightforward.) Usually, compilers can cope with that.
- To prevent this though, you can engage conditional compilation.
- Put at the head of PCXFILE.H:
-
- #ifndef _PCXFILE_ALREADY_INCLUDED_
- #define _PCXFILE_ALREADY_INCLUDED_
- ... // blah
-
- #endif
- at the end.
- This makes sure that the compilation of the body of PCXFILE.H is
- skipped, if this file already has been included.
-
- The second error (identifier unknown) seemingly has to do with the
- link stage.
- The compiler works thus:
-
- (SOURCE.C + SOURCE.H + INCLUDE.H) -(compile)-> SOURCE.O,
-
- where SOURCE.O is an object-code file, roughly machine code.
- The linker makes
-
- (SOURCE1.O + SOURCE2.O + ...) -(link)-> MYPROG.EXE
-
- an exe-file from all the object files.
- But at link time, every single object file must known about all the
- variables and functions of all the other object files it invokes.
- Thus, if you've omitted to include SOURCE2.H in the compile stage,
- the functions of SOURCE2.CPP will not be known at link stage.
-
- >So here are my questions:
- >
-
- Did I answer a few of them? I'm not sure myself, but if I'm wrong,
- I'll benefit from the corrections from wiser people, too.
-
- Elmar
-
- --
- --
- /----------------------------------------------------------------\
- | Elmar Vogt : MS->PhD | voice: (+49)9131/761-207; fax: ...-202 |
- | LUR, Schottkystr. 10 |http://www.rrze.uni-erlangen.de/~iwur08/ |
- |University of Erlangen| Elmar.Vogt@rzmail.uni-erlangen.de |
- |91058 Erlangen-Germany| Vogt@nt.e-technik.uni-erlangen.de |
- |-------- VIS VISCERIS, NON FERRO FERTUR (T. Doom) --------------|
- |SCA: Agilmar von Sevelingen; Fencing, Calligraphy, Jewelry, Food|
- \----------------------------------------------------------------/
-
-